package com.codeoss.framework.view.parser;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.codeoss.framework.data.metadata.Column;
import com.codeoss.framework.data.metadata.Table;
import com.codeoss.framework.data.store.ITableStore;
import com.codeoss.framework.expression.Expression;
import com.codeoss.framework.expression.ExpressionUtil;
import com.codeoss.framework.util.StringUtils;
import com.codeoss.framework.view.component.Component;
import com.codeoss.framework.view.component.Hidden;
import com.codeoss.framework.view.datasource.DataSourceConfig;
import com.codeoss.framework.view.util.DataSourceUtil;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;

@Service
public class HiddenParser extends AbstractComponentParser {
	@Autowired
	private ITableStore tableStore;
	@Autowired
	private DataSourceUtil dataSourceUtil;
	protected void setAttributes(IParseContext parseContext, ObjectNode objectNode, Component component) {
		super.setAttributes(parseContext, objectNode, component);
		Hidden hidden = (Hidden) component;
		ObjectNode propertyMapNode=(ObjectNode)objectNode.get("propertyMap");
		if(propertyMapNode!=null) {
			JsonNode jsonNode=propertyMapNode.get("dataSource");
			if(jsonNode!=null) {
				String dataSource=propertyMapNode.get("dataSource").asText();
				hidden.setDataSource(dataSource);
			}
			jsonNode=propertyMapNode.get("dataField");
			if(jsonNode!=null) {
				String dataField=propertyMapNode.get("dataField").asText();
				hidden.setDataField(dataField);
				
				DataSourceConfig dataSourceConfig = dataSourceUtil.getDataSourceConfigById(parseContext.getPage(),
						hidden.getDataSource());
				Table table = tableStore.get(dataSourceConfig.getTableName());
				Column column=table.getColumnByColumnName(dataField);
				
				String defaultValueExpression=column.getDefaultValueExpression();
				if(!StringUtils.isEmpty(defaultValueExpression)) {
					Expression expression=ExpressionUtil.convertToExpression(defaultValueExpression, Expression.EXPRESSION_TYPE_JAVASCRIPT);
					hidden.setDefaultValueExpression(expression);
				}
				
				hidden.setDataType(column.getDataType());
				
				jsonNode=propertyMapNode.get("varName");
				String varName="";
				if(jsonNode==null ||StringUtils.isEmpty(jsonNode.asText())) {
					if(!StringUtils.isEmpty(hidden.getDataSource()) && !StringUtils.isEmpty(hidden.getDataField())) {
						varName=table.getTableName()+"_"+hidden.getDataField();
					}
				}else {
					varName=propertyMapNode.get("varName").asText();
				}
				hidden.setVarName(varName);
			}
			
			
			jsonNode=propertyMapNode.get("dataType");
			if(jsonNode!=null) {
				String dataType=propertyMapNode.get("dataType").asText();
				hidden.setDataType(dataType);
			}
		}
	}
	
	public void afterPageParse(IParseContext parseContext,Component component) {
		Hidden hidden = (Hidden) component;
		 DataSourceConfig dataSourceConfig=dataSourceUtil.getDataSourceConfigById(parseContext.getPage(), hidden.getDataSource());
		 if(dataSourceConfig==null) {
			 return;
		 }
		 Table table= tableStore.get(dataSourceConfig.getTableName());
		 Column column=table.getColumnByColumnName(hidden.getDataField());
		 if(column!=null) {
			 Expression defaultValueExpression=hidden.getDefaultValueExpression();
			 if(defaultValueExpression!=null) {
				 parseExpression(parseContext, component, defaultValueExpression, "defaultValue", false);
			 }
		 }
		 super.afterPageParse(parseContext, component);
	}
	@Override
	protected Component createComponent(IParseContext parseContext, ObjectNode objectNode) {
		// TODO Auto-generated method stub
		return new Hidden();
	}

}
